/**
 * 
 */
package com.net.controller;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.net.annotaion.ApiLog;
import com.net.controller.model.ResponseModel;
import com.net.controller.model.UserParam;
import com.net.controller.vo.EditWebsiteParam;
import com.net.controller.vo.PWordAndSalt;
import com.net.controller.vo.UserListResponse;
import com.net.controller.vo.WebsiteResponseModel;
import com.net.model.User;
import com.net.model.Website;
import com.net.service.user.UserService;
import com.net.service.vo.ServiceCode;
import com.net.util.PasswordHash;
import com.net.util.ResponseEntityHelper;

/**
 *
 * @author fengcheng
 * @date 2018年6月16日 下午4:04:23
 */
@Controller
@RequestMapping(value = "/net")
public class UserApi  extends SuperController {
	UserService userService;

	/**
	 * @param userService
	 *            the userService to set
	 */
	@Resource(name = "userService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@ApiLog
	@RequestMapping(value = "/user/tolistpage")
	public ModelAndView toUserListPage(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		
		mv.setViewName("user/user_manage");
		return mv;
	}
	@ApiLog
	@RequestMapping(value = "/user/menu")
	public ModelAndView toMenutPage(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		
		mv.setViewName("menu");
		return mv;
	}

	@RequestMapping(value = "/user/register", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> register(@RequestBody UserParam param, HttpServletRequest request) {
		if (param.getUser_name() == null | param.getUser_name() == "") {
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "姓名不能为空");
		}
		if (param.getUser_pass() == null || param.getUser_pass() == "") {
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "密码不能为空");
		}
		User user = null;
		user = userService.getByName(param.getUser_name());
		if (user != null) {
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "该用户已经注册");
		}
		param.setType(1);
		userService.insertUser(param);
		return ResponseEntityHelper.createResponse(ServiceCode.SUCCESS, "注册成功");
	}
	@RequestMapping(value = "/user/login", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> login( HttpServletRequest request) {
		UserParam param=new UserParam();
		param.setUser_name(request.getParameter("user_name"));
		param.setUser_pass(request.getParameter("user_pass"));
		if (param.getUser_name() == null | param.getUser_name() == "") {
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "姓名不能为空");
		}
		if (param.getUser_pass() == null || param.getUser_pass() == "") {
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "密码不能为空");
		}
		User user = null;
		//TODO
		user = userService.getByName(param.getUser_name());
		try {
			if (user == null || !PasswordHash.validatePassword(param.getUser_pass(), user.getUser_pass())) {
				return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "用户名或密码错误");
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpSession session=request.getSession();
		session.setAttribute("user", user);
		return ResponseEntityHelper.createResponse(ServiceCode.SUCCESS, "登陆成功");
	}
	
	
	
	@ApiLog
	@RequestMapping(value = "/user/listall", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> listPageUsers(HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		List<User> userList = userService.getAll();
		UserListResponse ulr = new UserListResponse();
		ulr.setUserList(userList);
		return ResponseEntityHelper.createResponse(ServiceCode.SUCCESS, "获取成功",ulr);
	}
	
	
	@ApiLog
	@RequestMapping(value = "/user/edit", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> editUser(@RequestBody UserParam param, HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(param.getId() == 0 || "".equals(param.getId())){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "参数错误");
		}
		if(param.getUser_name() == null || "".equals(param.getUser_name())){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "请输入用户名");
		}
		
		if(param.getUser_pass() == null || "".equals(param.getUser_pass())){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "请输入密码");
		}else{
			PWordAndSalt pWordAndSalt = new PWordAndSalt();
			try {
				pWordAndSalt = PasswordHash.createHash(param.getUser_pass());
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidKeySpecException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			param.setUser_pass(pWordAndSalt.getPassword());
		}
		
		if(param.getType() == null || "".equals(param.getType())){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "请输入用户状态");
		}
		if(param.getStatus() == null || "".equals(param.getStatus())){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "请输入用户类型");
		}
		if(userService.editUser(param) == true){
			return ResponseEntityHelper.createResponse(ServiceCode.SUCCESS, "修改成功");
		}
		return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "服务器异常，用户信息修改失败");
	}
	
	
	@ApiLog
	@RequestMapping(value = "/user/del", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> delUser(@RequestBody UserParam param, HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(param.getId() == null || "".equals(param.getId())){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "参数错误");
		}
		if(userService.delUser(param.getId()) == true){
			return ResponseEntityHelper.createResponse(ServiceCode.SUCCESS, "删除成功");
		}
		return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "服务器异常，删除失败");
	}
	
	
	@ApiLog
	@RequestMapping(value = "/user/add", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> addWebsite(@RequestBody UserParam param, HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(param.getUser_name() == null || "".equals(param.getUser_name())){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "请输入用户名");
		}
		if(param.getUser_pass() == null || "".equals(param.getUser_pass())){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "请输密码");
		}else{
			PWordAndSalt pWordAndSalt = new PWordAndSalt();
			try {
				pWordAndSalt = PasswordHash.createHash(param.getUser_pass());
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidKeySpecException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			param.setUser_pass(pWordAndSalt.getPassword());
		}
		
		if(param.getType() == null || "".equals(param.getType())){
			param.setType(1);
		}
		
		if(param.getStatus() == null || "".equals(param.getStatus())){
			param.setStatus(0);
		}
		userService.insertUser(param);
		return ResponseEntityHelper.createResponse(ServiceCode.SUCCESS, "新增成功");
	}
}
